package com.example.lib_common;


import android.util.Log;



public class TLog {
    public static final String TAG_PRE = "TestDemo_ ";

    public static void log(int level, String tag, String msg, Throwable tr) {
        if (level == Log.VERBOSE && BuildConfig.DEBUG) {
            Log.v(tag, msg);
        } else if (level == Log.DEBUG && BuildConfig.DEBUG) {
//            Log.d(tag, msg);
            // 平台logcat有概率不打印debug级别的日志，强制把debug日志输出成info日志
            Log.i(tag, msg);
        } else if (level == Log.INFO && BuildConfig.DEBUG) {
            Log.i(tag, msg);
        } else if (level == Log.WARN &&BuildConfig.DEBUG) {
            Log.w(tag, msg);
        } else if (level == Log.ERROR && BuildConfig.DEBUG) {
            if (tr == null) {
                Log.e(tag, msg);
            } else {
                Log.e(tag, msg, tr);
            }
        }
    }

    public static void v(String tag, String msg) {
        log(Log.VERBOSE, TAG_PRE + tag, msg, null);
    }

    public static void d(String tag, String msg) {
        log(Log.DEBUG, TAG_PRE + tag, msg, null);
    }

    public static void i(String tag, String msg) {
        log(Log.INFO, TAG_PRE + tag, msg, null);
    }

    public static void w(String tag, String msg) {
        log(Log.WARN, TAG_PRE + tag, msg, null);
    }

    public static void e(String tag, String msg) {
        log(Log.ERROR, TAG_PRE + tag, msg, null);
    }

    public static void e(String tag, String msg, Throwable tr) {
        log(Log.ERROR, TAG_PRE + tag, msg, tr);
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

}
